package io.jee.runshan.controller.admin;

import io.jee.medusa.orm.condition.Condition;
import io.jee.medusa.orm.condition.ConditionGroup;
import io.jee.medusa.orm.pager.Pager;
import io.jee.runshan.domain.KeywordAsk;
import io.jee.runshan.service.IKeywordAskService;
import io.jee.runshan.utils.Utils;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 关键字回复Controller
 * @author SunX
 *
 */
@Controller
@RequestMapping("/admin/keywordAsk/")
public class KeywordAskController {
	
	private static final String LIST = "admin/keywordAsk/list";
	private static final String KEY_ATTR = "admin/keywordAsk/keyAttribute";
	private static final String FIXED_ATTR = "admin/keywordAsk/fixedAttribute";
	private static final String REDIRECT_LIST = "redirect:/admin/keywordAsk/list";

	@Resource
	private IKeywordAskService keywordAskService;
	
	/**
	 * 关键字列表
	 * 
	 * @param pager
	 * @param keywordAsk
	 * @return
	 */
	@RequestMapping("list")
	public ModelAndView list(Pager<KeywordAsk> pager, KeywordAsk keywordAsk, String keywords) {
		ConditionGroup group = new ConditionGroup();
		if (!Utils.isNullOrEmpty(keywordAsk.getType())) {
			group.and(Condition.onNumber("type").eq(keywordAsk.getType()));
		}
		if (!Utils.isNullOrEmpty(keywords)) {
			group.and(Condition.onText("keyword").like("%" + keywords + "%"));
			group.or(Condition.onText("title").like("%" + keywords + "%"));
		}
		pager = keywordAskService.list(pager, group);
		return new ModelAndView(LIST).addObject("pager", pager).addObject("keywordAsk", keywordAsk).addObject("keywords", keywords);
	}

	/**
	 * 编辑关键字
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("edit")
	public ModelAndView edit(Integer id, KeywordAsk keywordAsk) {
		if (!Utils.isNullOrEmpty(id)) {
			keywordAsk = keywordAskService.get(id);
		}
		String attr = KEY_ATTR;
		if(keywordAsk.getType() == KeywordAsk.TYPE_FIXED){
			 attr = FIXED_ATTR;
		}
		return new ModelAndView(attr).addObject("keywordAsk", keywordAsk);
	}

	/**
	 * 保存关键字
	 * 
	 * @param keywordAsk
	 * @return
	 */
	@RequestMapping("save")
	public ModelAndView save(KeywordAsk keywordAsk) {
		String msg = null;
		try {
			if (Utils.isNullOrEmpty(keywordAsk.getId())) {
				Integer id = (Integer) keywordAskService.save(keywordAsk);
				keywordAsk.setId(id);
				msg = "新增成功！";
			} else {
				//KeywordAsk serKeywordAsk = keywordAskService.load(keywordAsk.getId());
				//BeanUtils.copyProperties(keywordAsk, serKeywordAsk);
				keywordAskService.update(keywordAsk);
				msg = "修改成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "未知错误！" + e.getMessage();
		}
		String attr = KEY_ATTR;
		if(keywordAsk.getType() == KeywordAsk.TYPE_FIXED){
			 attr = FIXED_ATTR;
		}
		return new ModelAndView(attr).addObject("keywordAsk", keywordAsk).addObject("msg", msg);
	}

	/**
	 * 删除关键字
	 * 
	 * @param keywordAsk
	 * @return
	 */
	@RequestMapping("delete/{ids}")
	public String delete(@PathVariable Integer[] ids, Integer type,Model model) {
		String msg = null;
		Integer count = null;
		try {
			if (ids.length > 0) {
				count = keywordAskService.delete("id", ids);
				msg = count + "条记录删除成功！";
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg = "未知错误！" + e.getMessage();
		}
		model.addAttribute("msg",msg);
		return REDIRECT_LIST + "?type=" + type;
	}
}
